﻿package com.shootingGame.weaponsClasses
{
	import com.shootingGame.GamePlayManager;
	import com.common.utils.Math2;
	import com.shootingGame.Player;
	import com.shootingGame.renderableClasses.AttachableObject;
	import com.shootingGame.renderableClasses.GameObject;
	import com.shootingGame.weaponsClasses.launchProfiles.LaunchProfile;
	
	import flash.display.MovieClip;
	import flash.geom.Point;

	public class Weapon extends AttachableObject
	{
		public var type : int;
		private var _weaponInfo_Primary:WeaponInfo;
		private var _weaponInfo_Secondary:WeaponInfo;
		private var _isMouseAimAdjustEnabled : Boolean;
		private var _damageModifier : Number;
		private var _coolDownModifier : Number;
		private var _isAmmoShared : Boolean;
		private var _isTriggerDepressed : Boolean;
		private var _timeTriggerDepressed : int;
		
		public function Weapon(model:MovieClip, xPos:Number, yPos:Number, isMouseAimAdjustEnabled : Boolean, weaponInfo_Primary : WeaponInfo, weaponInfo_Secondary : WeaponInfo = null, isAmmoShared : Boolean = false)
		{
			super(model, xPos, yPos);
			isToBeRendered = true;
			isToBeUpdated = true;
			_weaponInfo_Primary = weaponInfo_Primary;
			_weaponInfo_Secondary = weaponInfo_Secondary;
			this._isAmmoShared = isAmmoShared;
			_isMouseAimAdjustEnabled = isMouseAimAdjustEnabled;
			_damageModifier = 1;
			_coolDownModifier = 1;
			GamePlayManager.instance.generalList.push(this);
		}
		
		override public function update():void{
			if(_isMouseAimAdjustEnabled)
				mouseAim();
			if(isTriggerDepressed)
				_timeTriggerDepressed += 1;
			else
				_timeTriggerDepressed = 0;
			super.update();
			weaponInfo_Primary.coolDownTimeCount -= _coolDownModifier;
			
		}
		
		public function mouseAim() : void {
			if(Player(attachedTo).isPrimaryPlayer){
				facing = Math2.getAngleOfElevationDegree(localToGlobal(preferredCentre).x, localToGlobal(preferredCentre).y, GamePlayManager.instance.mouseX, GamePlayManager.instance.mouseY);
				facing -= attachedTo.facing;
			}
//			var m:Matrix = new Matrix();
//			MatrixTransformer.rotateAroundInternalPoint(m, model.body.width/2.0, model.body.height/2.0, facing);
//			model.transform.matrix = m;
		}
		
		//Returns array of launched ordinance
		public function fire_Primary(targetX, targetY) : Array {
			var fireArray : Array;
			var globalPoint : Point = model.localToGlobal(new Point(model.nozzle.x, model.nozzle.y));
			var distance : Number = Math.sqrt(Math.pow(targetX - globalPoint.x, 2) + Math.pow(targetY - globalPoint.y, 2));
			
			if(weaponInfo_Primary.coolDownTimeCount <= 0){
				if(weaponInfo_Primary.accuracy < 1){
					targetX += distance * Math2.randFloat((weaponInfo_Primary.accuracy - 1)/2.0, (1-weaponInfo_Primary.accuracy)/2.0);
					targetY += distance * Math2.randFloat((weaponInfo_Primary.accuracy - 1)/2.0, (1-weaponInfo_Primary.accuracy)/2.0);
				}
				fireArray = LaunchProfile.launch(weaponInfo_Primary.launchprofile, this, weaponInfo_Primary, targetX, targetY);
				if(fireArray)
					weaponInfo_Primary.coolDownTimeCount = weaponInfo_Primary.coolDownTime;
			}
			return fireArray;
		}
		
		public function fire_Secondary(targetX, targetY) : Array {
			if(weaponInfo_Secondary)
				return LaunchProfile.launch(weaponInfo_Secondary.launchprofile, this, weaponInfo_Secondary, targetX, targetY);
			else
				return null;
		}
		
		public function get weaponInfo_Primary() : WeaponInfo{
			return _weaponInfo_Primary;
		}

		public function get weaponInfo_Secondary() : WeaponInfo{
			return _weaponInfo_Secondary;
		}		

		public function get damageModifier() : int{
			return _damageModifier;
		}
		
		public function set damageModifier(value : int) {
			_damageModifier = value;
		}
		
		public function get coolDownModifier() : Number{
			return _coolDownModifier;
		}
		
		public function set coolDownModifier(value : Number) {
			_coolDownModifier = value;
		}
		
		public function get isTriggerDepressed() : Boolean{
			return _isTriggerDepressed;
		}
		
		public function set isTriggerDepressed(value : Boolean) {
			_isTriggerDepressed = value;
		}
		
		public function get timeTriggerDepressed() : int{
			return _timeTriggerDepressed;
		}
		
		public function get isAmmoShared() : Boolean{
			return _isAmmoShared;
		}
		
		public function get nozzleX() : Number{
			return model.nozzle.x;
		}

		public function get nozzleY() : Number{
			return model.nozzle.y;
		}
		
		override public function set attachedTo(value:GameObject){
			super.attachedTo = value;
			weaponInfo_Primary.ordinanceSample.owner = attachedTo;
			//weaponInfo_Secondary.ordinanceSample.owner = attachedTo;
		}
	}
}
